import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Globals} from '../../../../../globals';
import {PageHelper} from "../../../../../utils/page-helper-model";
import {BaseService} from '../../../../../shared/service/base.service';
import {MyDateModel} from '../../../../../models/my-date.model';
import {ApiUrl} from '../../../../../config/api-url';
@Component({
  selector: 'app-log-mqtt',
  templateUrl: './log-mqtt.component.html',
  styleUrls: ['./log-mqtt.component.less']
})
export class LogMqttComponent implements OnInit {
  formModel: FormGroup;
  isMobile = Globals.isMobile();
  // 开始日期类型
  startDateTimeType = 'start';
  // 结束日期类型
  endDateTimeType = 'end';
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;
  listOfData: any;
  pageHelper: PageHelper = new PageHelper;
  constructor(private fb: FormBuilder, public baseService: BaseService) {
    this.buildForm();
  }
  ngOnInit(): void {
    this.startDateTime = this.baseService.zorro.getDateTime().startToday;
    this.endDateTime = this.baseService.zorro.getDateTime().endToday;
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findData();
  }
  buildForm() {
    this.formModel = this.fb.group({
      type: [-1, []],
      content: ['', []],
      createTime: [null, []],
      updateTime: [null, []],
      deviceNo: [null, []],
    });
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findData();
  }
  //查询
  submitForm() {
    this.pageHelper.currentPage = 1;
    this.findData();
  }
  findData() {
    this.formModel.get('createTime').setValue(this.startDateTime);
    this.formModel.get('updateTime').setValue(this.endDateTime);
    if(this.formModel.get('deviceNo').value == null || this.formModel.get('deviceNo').value == ''){
      this.baseService.layer.messageError('设备号不能为空');
      return;
    }
    if(this.formModel.get('createTime').value > this.formModel.get('updateTime').value){
      this.baseService.layer.messageError('开始时间不能大于结束时间');
      return;
    }
    this.baseService.findListByMuchCondition(ApiUrl.MessageLogRecordUrl + '/findMessageLogList', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  // 将开始、结束时间往外抛
  outDateTime($event: MyDateModel) {
    /**
     * date: Fri Mar 19 2021 23:59:06 GMT+0800 (中国标准时间) {}
     * type: "end"
     */
    if ($event.type == 'start') {
      this.startDateTime = $event.date;
    } else if ($event.type == 'end') {
      this.endDateTime = $event.date;
    }
  }
}
